package priv.happy.mealpickpro.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import priv.happy.mealpickpro.domain.Ranking;
import priv.happy.mealpickpro.emum.SystemStatusCode;
import priv.happy.mealpickpro.service.RankingService;
import priv.happy.mealpickpro.vo.ApiResponse;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: mealPickPro
 * @Package_name: priv.happy.mealpickpro.controller
 * @Date: 2023/10/21 19:46
 * @Description:
 */
@RestController
@RequestMapping("/api/v2/ranking")
public class RankingController {

    @Autowired
    RankingService rankingService;

    @Value("${ranking.limit}")
    Integer rankingLimit;

    /**
     * 获取排行榜信息
     *
     * @param type 获取的类型，标签，食物，选餐
     * @return
     */

    @GetMapping("/{type}")
    public ApiResponse<List<Ranking>> doGetAllRanking(@PathVariable String type) {
        ApiResponse<List<Ranking>> apiResponse = new ApiResponse<>();
        if (type != null) {
            QueryWrapper<Ranking> queryWrapper = new QueryWrapper<>();
            switch (type) {
                case "标签":
                    apiResponse.setData(rankingService.list(queryWrapper.eq("type", "标签")
                                    .likeRight("date", DateUtil.now().substring(0, 10))
                                    .orderByDesc("num").last("limit " + rankingLimit)))
                            .setCode(SystemStatusCode.SUCCESS.getCode()).setMessage("获取标签排行榜成功")
                            .setSuccess(true);
                    break;
                case "食物":
                    apiResponse.setData(rankingService.list(queryWrapper.eq("type", "食物")
                                    .likeRight("date", DateUtil.now().substring(0, 10))
                                    .orderByDesc("num").last("limit " + rankingLimit)))
                            .setCode(SystemStatusCode.SUCCESS.getCode()).setMessage("获取食物排行榜成功")
                            .setSuccess(true);
                    break;
                case "选餐":
                    apiResponse.setData(rankingService.list(queryWrapper.eq("type", "选餐")
                            .likeRight("date", DateUtil.now().substring(0, 10))
                                    .orderByDesc("num").last("limit " + rankingLimit)))
                            .setCode(SystemStatusCode.SUCCESS.getCode()).setMessage("获取选餐排行榜成功")
                            .setSuccess(true);
                    break;
                default:
                    apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).setMessage("拉取标签排行榜失败").setSuccess(false);
            }
        }

        return apiResponse;
    }
}
